package com.yuexun.dhdoctor.widget.model;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.bean.model.HelpFileData;
import com.yuexun.dhdoctor.baselistener.StringListener;
import com.yuexun.dhdoctor.widget.pickerUtil.ModelDatePicker;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by chenss on 2017/7/31 0031.
 */

public class CreateDateTimeView {

    Context context;
    ArrayList<HelpFileData> dtlList;//配置详情列表
    ModelDatePicker customDatePicker1;

    SimpleDateFormat sdf = null;
    String isDate;
    List<HelpFileData> alreadyDtlList;
    boolean isBackfill;

    public CreateDateTimeView(Context context, ArrayList<HelpFileData> dtlList, List<HelpFileData> alreadyDtlList, boolean isBackfill){
        this.context = context;
        this.dtlList = dtlList;
        this.alreadyDtlList = alreadyDtlList;
        this.isBackfill = isBackfill;
    }

    /**
     * 方法描述：从布局中添加日期布局
     *
     * @param fileData
     * @return
     */
    public View createDateTimeView(String[] typeData, final HelpFileData fileData, final String isDate) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(context).inflate(R.layout.add_item_datetime_layout_01, null);//也可以从XML中加载布局

        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        final TextView startDate = (TextView) view.findViewById(R.id.date_view);
        TextView tvView = (TextView) view.findViewById(R.id.tv_view);

        if(typeData[0].equals("9")){
            tvView.setVisibility(View.GONE);
        }
        if (isDate.equals("4")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        } else if (isDate.equals("3")){
            sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        }
        final String now = sdf.format(new Date());

        txt_title.setText(fileData.getDtlName());

        if(isBackfill){
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    startDate.setText(data.getDtlValue());
                }
            }
        }else {
            if(typeData[0].equals("5")){
                startDate.setText(now);
            }
        }

        fileData.setDtlValue(startDate.getText().toString());
        fileData.setShowType(typeData[0]);

        startDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(startDate, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        fileData.setDtlValue(position);
                    }
                });
            }
        });

        dtlList.add(fileData);
        return view;
    }

    /**
     * 方法描述：从布局中添加日期布局
     *
     * @param fileData
     * @return
     */
    public View createDateTime2View(final String[] typeData, final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(context).inflate(R.layout.add_item_datetime_layout_02, null);//也可以从XML中加载布局

        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        final TextView startDate = (TextView) view.findViewById(R.id.start_date_view);
        final TextView endDate = (TextView) view.findViewById(R.id.end_date_view);
        TextView tvView = (TextView) view.findViewById(R.id.tv_view);

        if(typeData[0].equals("9")){
            tvView.setVisibility(View.GONE);
        }
        if(typeData[1].equals("3")){
            sdf = new SimpleDateFormat("yyyy", Locale.CHINA);
            isDate = "1";
        }else if(typeData[1].equals("4")){
            sdf = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
            isDate = "2";
        }else if(typeData[1].equals("5")){
            sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            isDate = "3";
        }else if(typeData[1].equals("6")){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
            isDate = "4";
        }
        final String now = sdf.format(new Date());

        txt_title.setText(fileData.getDtlName());
        if(isBackfill){
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    String[] time = data.getDtlValue().replace("&nbsp;", " ").split(" -- ");
                    startDate.setText(time[0]);
                    endDate.setText(time[1]);
                }
            }
        }else {
            if(typeData[0].equals("5")){
                startDate.setText(now);
                endDate.setText(now);
            }
        }

        fileData.setDtlValue(startDate.getText().toString() + " -- " + endDate.getText().toString());
        fileData.setShowType(typeData[0]);
        startDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(startDate, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        fileData.setDtlValue(position + " -- " + endDate.getText().toString());
                    }
                });
            }
        });
        endDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(endDate, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        fileData.setDtlValue(startDate.getText().toString() + " -- " + position);
                    }
                });
            }
        });

        dtlList.add(fileData);
        return view;
    }


    private void initDatePicker(final TextView tv, final String num, final StringListener listener) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());

        customDatePicker1 = new ModelDatePicker(context, new ModelDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                tv.setText(time);
                listener.getBack(time);
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecific(num);
        customDatePicker1.setIsLoop(false); // 不允许循环滚动
        customDatePicker1.show(now);
    }

}
